#!/usr/bin/env bash

if [ "${SWTPM_TEST_EXPENSIVE:-0}" -eq 0 ]; then
	exit 77
fi

ROOT=${abs_top_builddir:-$(dirname "$0")/..}
TESTDIR=${abs_top_testdir:-$(dirname "$0")}
TESTDIR=$(realpath "${TESTDIR}")

function cleanup() {
	if [ -n "${SWTPM_PID}" ]; then
		kill -9 "${SWTPM_PID}"
	fi
	if [ -n "${SWTPM1_PID}" ]; then
		kill -9 "${SWTPM1_PID}"
	fi
	if [ -n "${WORKDIR}" ]; then
		rm -rf "${WORKDIR}"
	fi
	# clean up after (interrupted) test suite
	rm -f /tmp/.key-*-0 /tmp/.delegation-0 /tmp/.transdigest-*-0
}

trap "cleanup" EXIT

source "${TESTDIR}/common"
skip_test_no_tpm12 "${SWTPM_EXE}"

WORKDIR="$(mktemp -d)" || exit 1
TESTLOG=${WORKDIR}/test.log

# variables used by the TPM 1.2 test suite
TPM_SERVER_PORT=65440
TPM_SERVER_NAME=127.0.0.1
SLAVE_TPM_PORT=65442
SLAVE_TPM_SERVER=127.0.0.1

SWTPM_INTERFACE=socket+socket

# Start main TPM 1.2
SWTPM_SERVER_PORT=${TPM_SERVER_PORT}
SWTPM_SERVER_NAME=${TPM_SERVER_NAME}
SWTPM_CTRL_PORT=65441

mkdir -p "${WORKDIR}/tpm12.1"
SWTPM_SERVER_NO_DISCONNECT="1" run_swtpm "${SWTPM_INTERFACE}" \
	--tpmstate "dir=${WORKDIR}/tpm12.1" \
	--flags not-need-init
SWTPM1_PID=${SWTPM_PID}

# Start 2nd TPM 1.2
SWTPM_SERVER_PORT=${SLAVE_TPM_PORT}
SWTPM_SERVER_NAME=${SLAVE_TPM_SERVER}
SWTPM_CTRL_PORT=65443

mkdir -p "${WORKDIR}/tpm12.2"
SWTPM_SERVER_NO_DISCONNECT="1" run_swtpm "${SWTPM_INTERFACE}" \
	--tpmstate "dir=${WORKDIR}/tpm12.2" \
	--flags not-need-init

pushd "${WORKDIR}" &>/dev/null || exit 1

for ((i = 0; i < 3; i++)); do
	if curl -sJOL https://sourceforge.net/projects/ibmswtpm/files/tpm4769tar.gz/download; then
		break
	fi
	# wait 3..10 seconds for a retry
	sleep $((3 + (RANDOM & 7)))
done
hash=$(get_sha1_file tpm4769tar.gz)
if [ "$hash" != ca99a3ccd3e41cdd9983086a9b944023b6049bbc ]; then
	echo "Error: Downloaded file does not have expected hash."
	echo "Actual: $hash"
	exit 1
fi
tar -xzf tpm4769tar.gz

pushd libtpm &>/dev/null || exit 1

if ! patch -p0 < "${TESTDIR}/patches/libtpm.patch"; then
	echo "Error: Patching failed."
	exit 1
fi

./autogen
LIBS="" CFLAGS="-g -O2 -DOPENSSL_SUPPRESS_DEPRECATED=1" ./configure
make -j "$(nproc)"

pushd utils &>/dev/null || exit 1

# variables used by TPM 1.2 tools and test suite
export TPM_SERVER_PORT=${TPM_SERVER_PORT} \
    TPM_SERVER_NAME=${TPM_SERVER_NAME} \
    SLAVE_TPM_PORT=${SLAVE_TPM_PORT} \
    SLAVE_TPM_SERVER=${SLAVE_TPM_SERVER} \
    PATH=$PWD:$PATH

if wait_for_serversocket ${TPM_SERVER_PORT} 127.0.0.1 2; then
	echo "Error: swtpm 1 did not open port ${TPM_SERVER_PORT}"
	exit 1
fi

if wait_for_serversocket ${SLAVE_TPM_PORT} 127.0.0.1 2; then
	echo "Error: swtpm 2 did not open port ${SLAVE_TPM_PORT}"
	exit 1
fi

tpmbios

ln -s makeidentity identity

# keep test 1 last due to ERRORs it creates since we do not
# restart the TPM
for tst in 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 20 23 25 26 1; do

    TPM_AUDITING=0
    TPM_TRANSPORT=0

    echo "Running test ${tst}"
    if [[ "${tst}" =~ ^(1)$ ]]; then
        $SWTPM_IOCTL --tcp :65441 -i
        tpmbios
    elif [[ "${tst}" = ^25$ ]]; then
        TPM_AUDITING=1
        TPM_TRANSPORT=1
    fi

    TPM_AUDITING=${TPM_AUDITING} TPM_TRANSPORT=${TPM_TRANSPORT} timeout 40 ./test_console.sh \
        --non-interactive "${tst}" >> "${TESTLOG}" </dev/null
    # Ignore all errors that occurred in test 1
    if [ $tst != "1" ] && grep -q "ERROR" "${TESTLOG}"; then
        echo "Error occurred!"
        cat "${TESTLOG}"
        exit 1
    fi
done

$SWTPM_IOCTL --tcp :65441 -s
wait_process_gone "${SWTPM1_PID}" 4
SWTPM1_PID=

$SWTPM_IOCTL --tcp :65443 -s
wait_process_gone "${SWTPM_PID}" 4
SWTPM_PID=

popd &>/dev/null || exit 1
popd &>/dev/null || exit 1

echo "OK"

exit 0
